package biz.master.mgbean;

import biz.master.dto.ProductCategory;
import biz.master.service.ProductCategorySvr;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.context.RequestContext;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.List;


public class ProductCategoryMgb implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -2658816198129548602L;
    private Integer idSearch;
    private String nameSearch;
    private Integer activeSearch;
    private List<ProductCategory> resultSearch;

    private ProductCategory selected;
    private DataTable dtResult;
    ProductCategorySvr dao = new ProductCategorySvr();

    public ProductCategoryMgb() {
        super();
        this.reset();
    }

    public DataTable getDtResult() {
        return dtResult;
    }

    public void setDtResult(DataTable dtResult) {
        this.dtResult = dtResult;
    }


    public Integer getActiveSearch() {
        return activeSearch;
    }

    public void setActiveSearch(Integer activeSearch) {
        this.activeSearch = activeSearch;
    }

    public List<ProductCategory> getResultSearch() {
        return resultSearch;
    }

    public void setResultSearch(List<ProductCategory> resultSearch) {
        this.resultSearch = resultSearch;
    }

    public ProductCategory getSelected() {
        return selected;
    }

    public void setSelected(ProductCategory selected) {
        this.selected = selected;
    }

    public void add() {
        selected = new ProductCategory();
    }

    public void reset() {
        idSearch = null;
        nameSearch = null;
        selected = null;
    }

    public Integer getIdSearch() {
        return idSearch;
    }

    public void setIdSearch(Integer idSearch) {
        this.idSearch = idSearch;
    }

    public String getNameSearch() {
        return nameSearch;
    }

    public void setNameSearch(String nameSearch) {
        this.nameSearch = nameSearch;
    }

    public void search() {

        ProductCategory o = new ProductCategory();
        o.setId(idSearch);
        o.setName((nameSearch));
        resultSearch = dao.search(o);
    }

    public void view(ProductCategory value) {
        selected = value;
    }

    public boolean checkDup(String value) {
        ProductCategory item = new ProductCategory();
        item.setName(value);
        if (dao.search(item).size() == 0)
            return false;
        return true;
    }

    public void save() {
        FacesContext context = FacesContext.getCurrentInstance();

        if (selected.getId() != null)
            dao.update(selected);
        else {
            if (checkDup(selected.getName())) {
                RequestContext.getCurrentInstance().addCallbackParam(
                        "checkBusinessFail", true);
                context.addMessage("growl", new FacesMessage(
                        FacesMessage.SEVERITY_ERROR, "Add Error",
                        "Duplicate Name:" + selected.getName()));
                return;
            }
            dao.create(selected);
        }
        context.addMessage("growl", new FacesMessage("Operation Success",
                "Save ID: " + selected.getId()));
        search();

    }

    public void delete() {

        dao.delete(selected.getId());
        search();
        FacesContext.getCurrentInstance().addMessage(
                "growl",
                new FacesMessage("Operation Success", "Delete ID: "
                        + selected.getId()));
    }

}
